//
//
//
#include "portocolLogin.h"
#include <utf.h>

_LIT8(KInterfaceNo, "interfaceno");

_LIT8(KReqLoginURL, "http://58.246.187.214/client.php?ac=login");
_LIT(KReqLoginData, "username=%s&password=%s&token=%s&version=%s&devicetoken=\"\"");
_LIT(KReqInfoNone, "\"\"");

CRocketRequestLogin::CRocketRequestLogin(MNetworkEventObervser* aObserver) : CRocketRequest(aObserver)
	{
		iUrl.Copy(KReqLoginURL());
		iNumber = CMSGLOGIN;
	}

CRocketRequestLogin::~CRocketRequestLogin()
	{
		
	}

void CRocketRequestLogin::SetInfo(const TDesC& aName, const TDesC& aPassword, const TDesC& aToken, const TDesC& aVersion)
{
	iUserName.Zero();
	iPassword.Zero();
	iToken.Zero();
	iVersion.Zero();
	
	iUserName.Copy(aName);
	iPassword.Copy(aPassword);
	iToken.Copy(aToken);
	iVersion.Copy(aVersion);
}

TBool CRocketRequestLogin::Encode()
{
	TBuf<10> emBuf;
	emBuf.Copy(_L("\"\""));
	TBuf<512> buf;	
	buf.Format(KReqLoginData(),
				iUserName.PtrZ(),
				iPassword.PtrZ(),
				iToken.Length() > 0 ? iToken.PtrZ() : emBuf.PtrZ(),
				iVersion.Length() > 0 ? iVersion.PtrZ() : emBuf.PtrZ());
	TBuf8<1024> buf8;
	CnvUtfConverter::ConvertFromUnicodeToUtf8(buf8,buf);
	this->iData = buf8.Alloc();
	return ETrue;
}

CRocketResponseLogin::CRocketResponseLogin()
	{
	
	}

CRocketResponseLogin::~CRocketResponseLogin()
	{
	
	}

void CRocketResponseLogin::OnEndElementL(const Xml::RTagInfo &aElement, 
										TInt aErrorCode)
{
	
}

// XML tags
_LIT8(KLoginTag, "login");
_LIT8(KRetTag, "ret");
_LIT8(KUidTag, "uid");
_LIT8(KRandTag, "rand");
_LIT8(KNickName, "nickname");
_LIT8(KHeadPic, "headpic");
_LIT8(KBGrade,	"bgrade");
_LIT8(KSGrade,	"sgrade");
_LIT8(KMood,	"mood");
_LIT8(KBulletin, "bulletin");
_LIT8(KNewGiftNum, "newgiftnum");
_LIT8(KAppUrl,	"appurl");

void CRocketResponseLogin::OnStartElementL(const Xml::RTagInfo& aElement,
						const Xml::RAttributeArray& aAttributes,
						TInt aErrorCode)
{
	if(aElement.LocalName().DesC() == KInterfaceNo)
	{
		
	}
	else if(aElement.LocalName().DesC() == KLoginTag)
	{
		for(TInt nIndex = 0; nIndex < aAttributes.Count(); nIndex ++)
		{
			//Xml::RAttribute attr = aAttributes.At(nIndex);
			if(0 == aAttributes[nIndex].Attribute().LocalName().DesC().Compare(KRetTag))
			{
				TLex8 aLex(aAttributes[nIndex].Value().DesC());
				aLex.Val(iLogInfo.ret);
			}
			else if(0 == aAttributes[nIndex].Attribute().LocalName().DesC().Compare(KUidTag))
			{
				iLogInfo.uid.Copy(aAttributes[nIndex].Value().DesC());
			}
			else if(0 == aAttributes[nIndex].Attribute().LocalName().DesC().Compare(KRandTag))
			{
				iLogInfo.rand.Copy(aAttributes[nIndex].Value().DesC());
			}
			else if(0 == aAttributes[nIndex].Attribute().LocalName().DesC().Compare(KNickName))
			{
				iLogInfo.rand.Zero();
				TBuf8<128> buf;
				buf.Zero();
				buf.Copy(aAttributes[nIndex].Value().DesC());
				CnvUtfConverter::ConvertToUnicodeFromUtf8(iLogInfo.nickname,buf);
			}
			else if(0 == aAttributes[nIndex].Attribute().LocalName().DesC().Compare(KHeadPic))
			{
				iLogInfo.headpicUrl.Copy(aAttributes[nIndex].Value().DesC());
			}
			else if(0 == aAttributes[nIndex].Attribute().LocalName().DesC().Compare(KBGrade))
			{
				TLex8 lex(aAttributes[nIndex].Value().DesC());
				lex.Val(iLogInfo.bgrade);
			}
			else if(0 == aAttributes[nIndex].Attribute().LocalName().DesC().Compare(KSGrade))
			{
				TLex8 lex(aAttributes[nIndex].Value().DesC());
				lex.Val(iLogInfo.sgrade);
			}
			else if(0 == aAttributes[nIndex].Attribute().LocalName().DesC().Compare(KMood))
			{
				iLogInfo.mood.Zero();
				TBuf8<128> buf;
				buf.Zero();
				buf.Copy(aAttributes[nIndex].Value().DesC());
				CnvUtfConverter::ConvertToUnicodeFromUtf8(iLogInfo.mood,buf);
			}
			else if(0 == aAttributes[nIndex].Attribute().LocalName().DesC().Compare(KNewGiftNum))
			{
				TLex8 lex(aAttributes[nIndex].Value().DesC());
				lex.Val(iLogInfo.newgiftNum);
			}	
			else if(0 == aAttributes[nIndex].Attribute().LocalName().DesC().Compare(KAppUrl))
			{
				TLex8 lex(aAttributes[nIndex].Value().DesC());
				lex.Val(iLogInfo.newmsgNum);
			}
		}
	}
}
